package com.maduar.demo.thread;

/**
 * Description:
 *
 * @author danny.pang danny.pang@ebizprise.com
 * @date 2020-06-21 16:15
 */
public class Ticket implements Runnable {


    /**
     * @Author: danny.pang
     * @Date: 2020-06-21 16:16
     * @Discription: 出票数
     */
    private volatile int num = 0;

    /**
     * @Author: danny.pang
     * @Date: 2020-06-21 16:17
     * @Discription: 剩余票数
     */
    private int count = 20;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (count <= 0) {
                    // 没有余票，跳出循环
                    break;
                }

                num++;
                count--;

                System.out.println("显示出票信息： " + Thread.currentThread().getName()
                        + "抢到第" + num + "张票, 剩余" + count + "张票");
            }
        }
    }

    public static void main(String[] args) {
        Ticket t1 = new Ticket();

        Thread j = new Thread(t1, "jacky");
        Thread n = new Thread(t1, "nano");

        Thread m = new Thread(t1, "mary");

        m.start();
        j.start();
        n.start();
    }
}
